package com.hqyj.freemovie.front.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.freemovie.front.domain.Movie;
import com.hqyj.freemovie.front.domain.MovieTags;
import com.hqyj.freemovie.front.mapper.MovieMapper;
import com.hqyj.freemovie.front.mapper.MovieTagsMapper;
import com.hqyj.freemovie.front.service.MovieService;
import com.hqyj.freemovie.front.service.MovieTagsService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class MovieServiceImpl extends ServiceImpl<MovieMapper, Movie> implements MovieService {

    @Resource
    private MovieMapper movieMapper;

    @Resource
    private MovieTagsMapper movieTagsMapper;

    @Resource
    private MovieTagsService movieTagsService;



    @Value("${freemovie.perPageSize}")
    private Integer perPagerSize;

    @Transactional
    @Override
    public boolean saveMovie(Movie movie) throws Exception {
        try {
            movieMapper.insert(movie);

            //保存电影的标签；
            if (movie.getMovieTagsList() != null && movie.getMovieTagsList().size() > 0) {
                List<MovieTags> movieTagsList = new ArrayList<MovieTags>();
                for (String tag : movie.getMovieTagsList()) {
                    MovieTags movieTags = new MovieTags();
                    movieTags.setMId(movie.getId());
                    movieTags.setTags(tag);
                    movieTagsList.add(movieTags);
                }
                return movieTagsService.saveBatch(movieTagsList);
            }
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }

    }

    @Transactional
    @Override
    public boolean updateMovie(Movie movie) throws Exception {
        try{
            movieMapper.updateById(movie);

            //把原来的电影标签一律删除....
            //学习下mybatisplus原生删除怎么做。
            movieTagsMapper.removeMovieTagsById(movie.getId());

            //保存电影的标签；
            if (movie.getMovieTagsList() != null && movie.getMovieTagsList().size() > 0) {
                List<MovieTags> movieTagsList = new ArrayList<MovieTags>();
                for (String tag : movie.getMovieTagsList()) {
                    MovieTags movieTags = new MovieTags();
                    movieTags.setMId(movie.getId());
                    movieTags.setTags(tag);
                    movieTagsList.add(movieTags);
                }
                return movieTagsService.saveBatch(movieTagsList);
            }
            return true;
        }catch (Exception ex){
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }

    }

    @Transactional
    @Override
    public boolean removeMovie(Integer id) throws Exception {
        return false;
    }

    @Override
    public Movie queryMovieById(Integer id) {
        QueryWrapper<Movie> movieQueryWrapper = new QueryWrapper<Movie>();
        movieQueryWrapper.eq("id", id);
        Movie movie = movieMapper.selectOne(movieQueryWrapper);
        List<MovieTags> movieTagsList = movieTagsService.queryMovieTagsByMovieId(id);
        if(movieTagsList!=null && movieTagsList.size()>0){
            List<String> tagsList = new ArrayList<String>();
            for(MovieTags tag : movieTagsList){
                tagsList.add(tag.getTags());
            }
            movie.setMovieTagsList(tagsList);
        }


        return movie;
    }

    @Override
    public List<Movie> queryMovieListByType(String type) {
        return null;
    }

    @Override
    public Page<Movie> queryMoviePagerByType(String type, String keyword, Integer currentPageNumber) {
        QueryWrapper<Movie> queryWrapper = new QueryWrapper<Movie>();
        if (!"全部".equals(type)) {
            //queryWrapper = new QueryWrapper<Movie>();
            queryWrapper.eq("type", type);
        }
        queryWrapper.like("name", keyword);

        Page<Movie> page = new Page(currentPageNumber, perPagerSize);

        return movieMapper.selectPage(page, queryWrapper);
    }
}
